/************************************************************************/
// Copyright (C) 2016, han_gangbiao. All rights reserved.
// Use of this source code is governed by a GPL-v2 license that can be found in the License file.
// 
// [Time]:      2016-1-4 21:53:17
// [Author]:    han_gangbiao [http://my.oschina.net/jackhen]
// [Info]:      
/************************************************************************/

#include "ppclient.h"
#include "protocol/net_binary_pdu.h"
#include "protocol/net_proto_def.h"

extern int g_blocksize; //消息块大小
extern int g_totalCount;//总次数
//extern int g_curCount;  //已经测试的次数

net_reactor_t g_reactor_engine;

int main(int argc, char* argv[])
{
    const char* ip = NULL;
    int port = 0;
    if(argc > 3) {
        ip = argv[1];
        port = atoi(argv[2]);
        g_blocksize = atoi(argv[3]);
        
        g_totalCount = argc > 4 ? atoi(argv[4]) : 10000;
        
    } else {
        fprintf(stderr, "Usage: %s server_ip server_port block_size", argv[0]);
        fprintf(stderr, " [msg_totalCount]\n"); 
        return -1;
    }
    
    if(port <= 0 || port >= 65536)
    {
        fprintf(stderr, "Usage: %s server_ip server_port block_size", argv[0]);
        fprintf(stderr, " [msg_totalCount]\n"); 
        return -1;
    }
    

	CPPClient* ppClient = new CPPClient(&g_reactor_engine);
	
    //CPPClient* ppClient_B = new CPPClient(&g_reactor_engine); //复用引擎
    
    g_reactor_engine.init_reactor(ppClient, 4);
	ppClient->startConnect(ip, port);

	//std::string str = "Net Reactor TPS Test!";

	char c;
	do{
		c = getchar();
		SLEEP_MS(100);

		if(c == 's' || c == 'S')
		{
			std::string msg = "";
			for(int i=0; i<g_blocksize; ++i)
			{
                msg.push_back('h');
				//msg.push_back(static_cast<char>(i%128));
			}
            smart::ref_ptr<NetBinaryPdu> pdu = new NetBinaryPdu();
            pdu->setUnit(1);
            pdu->setFlag(NET_PDU_REQUEST);
            pdu->setOperation(10);
            pdu->setSeq(2);
            pdu->setBody(msg.c_str());
            //pdu.toStream();
            ppClient->tag_time_and_count(); // 标记开始时间
            ppClient->send_pdu(pdu);
			//ppClient->sendData(msg.c_str(), msg.length());
		}

		printf("Press q or Q to exit...\n");
	}while(c!='q' && c!='Q');

	g_reactor_engine.uninit_reactor();
    SAFE_DELETE(ppClient);
	return 0;
}


